<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="./script/rem.js"></script>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/aui-tab.js" ></script>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
    <script type="text/javascript" src="./script/aui-sharebox.js" ></script>
</head>
<body>

    <header class="uplevel-head" style="padding-top:0.4rem;">
        <div class="head-l" tapmode onclick="closeWin()"><i class="fa fa-angle-left"></i></div>
        <div class="uplevel-head-m" id="sort">
            <span id="apply" onclick="changeSort('apply')" >权限提升</span>
            <span id="auditing" onclick="changeSort('auditing')" >权限审批</span>
        </div>
        <div class="head-r"></div>
    </header>
    <div class="head-d" style="padding-top:0.4rem;"></div>
    <div id="list">

    </div>
<!--     <dl class="uplevel-apply-t">
        <dt><img src="image/gril.png"><p>当前级别：<span>MVP</span></p></dt>
        <dd>当前您可以升级到白领、金领、BOSS</dd> 
    </dl>
    <div class="uplevel-apply-m">
        <i></i>
        <span>我要升级到</span>
        <i></i>
    </div>
    <ul class="uplevel-apply-list">
        <li>
            白领
            <span>升级</span>
        </li>
        <li>
            金领
            <span>升级</span>
        </li>
        <li>
            BOSS
            <span>升级</span>
        </li>
    </ul> -->

<!--     <ul class="uplevel-examine">
        <li>
            <div class="uplevel-examine-l">
                <img src="image/gril.png">
                <dl>
                    <dt>张春华</dt>
                    <dd>手机号：564878788784</dd>
                </dl>
            </div>
        </div>
        <div class="uplevel-examine-r">
            申请升级到
            <p>白领</p>
        </div>
    </li>

    <li>
        <div class="uplevel-examine-l">
            <img src="image/gril.png">
            <dl>
                <dt>张春华</dt>
                <dd>手机号：564878788784</dd>
            </dl>
        </div>
    </div>
    <div class="uplevel-examine-r">
        申请升级到
        <p>白领</p>
    </div>
</li>

<li>
    <div class="uplevel-examine-l">
        <img src="image/gril.png">
        <dl>
            <dt>张春华</dt>
            <dd>手机号：564878788784</dd>
        </dl>
    </div>
</div>
<div class="uplevel-examine-r">
    申请升级到
    <p>白领</p>
</div>
</li>
</ul> -->


</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/index.js"></script>
<script type="text/template" id="template_apply">
    <dl class="uplevel-apply-t">
        <dt><img src="{{=it.avatar}}"><p>当前级别：<span>{{=it.level_str}}</span></p></dt>
        <dd>当前您可以升级到
            {{~it.level_to:value:index}}
            {{=value.string}}、
            {{~}}
        </dd> 
    </dl>
    <div class="uplevel-apply-m">
        <i></i>
        <span>我要升级到</span>
        <i></i>
    </div>
    <ul class="uplevel-apply-list">
        {{~it.level_to:value:index}}
        {{? value.apply_id == 0}}
        <li>
            {{=value.string}}
            <span onclick="handleUpLevel('{{=value.level}}');">升级</span>
        </li>
        {{??}}
        <li>
            {{=value.string}}
            <span onclick="cancelUpLevel('{{=value.apply_id}}');">撤回申请</span>
        </li>
        {{?}}
        {{~}}
    </ul>
</script>
<script type="text/template" id="template_auditing">
    <ul class="uplevel-examine">
        {{~it:value:index}}
        <li onclick="handleAuditing('{{=value.name}}','{{=value.level_str}}','{{=value.id}}','{{=value.level_integral}}','{{=value.avatar}}');">
            <div class="uplevel-examine-l">
                <img src="image/gril.png">
                <dl>
                    <dt>{{=value.name}}</dt>
                    <dd>手机号：{{=value.phone}}</dd>
                </dl>
            </div>

            <div class="uplevel-examine-r">
                申请提升到
                <p>{{=value.level_str}}</p>
            </div>
        </li>
        {{~}}
    </ul>
</script>
<script type="text/javascript">

    apiready = function(){

        var uplevel_apply = $api.getStorage('uplevel_apply');
        if (uplevel_apply == 1) {
            sortName = 'auditing';
            $('#sort span').removeClass('active');
            $('#auditing').addClass('active');
            loadAuditing();
            $api.setStorage('uplevel_apply', null);     
        }else{

            sortName = 'apply';
            $('#sort span').removeClass('active');
            $('#apply').addClass('active');
            loadApply();
        }
        reloadListener();
    }

    // 加载升级申请
    function loadApply(){

        // 进度提示框
        api.showProgress({
          title: '努力加载中...',
          text: '请稍等...',
          modal: false
      });
        
        // 获取当前用户级别
        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/auth/info',
            method: 'get',
            data: {
                values: {
                },
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                api.hideProgress();
                if (ret.data) {
                    templateFun(ret.data,1);
                }
            } else {
                // alert( JSON.stringify( err ) );
                api.hideProgress();
                return false;
            }
        });

    }

    // 加载升级审批
    function loadAuditing(){

        // 进度提示框
        api.showProgress({
          title: '努力加载中...',
          text: '请稍等...',
          modal: false
      });

        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/user/upgrade/approval/list',
            method: 'get',
            data: {
                values: {
                },
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                api.hideProgress();
                if (ret.data) {
                    templateFun(ret.data,2);
                }
            } else {
                // alert( JSON.stringify( err ) );
                api.hideProgress();
                return false;
            }
        });
    }

    // 模板赋值函数
    function templateFun(data,type){

        if (type == 1) {

            // 升级申请 数据为空
            if (!data) {
             var list = $api.byId('list');
             var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:0.5rem;">暂无可升级申请</h2>';
             $api.html(list, html);
         }else{
            if (type == 1) {
               template = $api.byId('template_apply');           
           }else{
            template = $api.byId('template_auditing');           
        }

        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var list = $api.byId('list');
        $api.html(list, html);

    }
}else{

    // 升级审批 数据为空
    if (!data.length) {
     var list = $api.byId('list');
     var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:0.5rem;">暂无升级审批</h2>';
         // list.innerHTML = html;
         $api.html(list, html);
     }else{
        if (type == 1) {
           template = $api.byId('template_apply');           
       }else{
        template = $api.byId('template_auditing');           
    }

    var dotFn = doT.template(template.innerHTML);
    var html = dotFn(data);
    var list = $api.byId('list');
    $api.html(list, html);

}
}
}

    // 处理升级按钮
    function handleUpLevel(level){
        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/user/upgrade/apply',
            method: 'post',
            data: {
                values: {
                    level: level,
                },
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                if (ret.status_code == 200) {
                    api.openWin({
                        name: 'uplevel_submit',
                        url: './uplevel_submit.html',
                        pageParam: {
                            name: 'test'
                        }
                    });
                }
            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 撤回升级申请
    function cancelUpLevel(apply_id){
        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/user/upgrade/cancel',
            method: 'post',
            data: {
                values: {
                    apply_id: apply_id,
                },
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                if (ret.status_code == 200) {
                    api.toast({
                        msg: ret.message,
                        duration: 2000,
                        location: 'bottom'
                    });
                    loadApply();
                }
            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 跳转审批详情
    function handleAuditing(name,level_str,id,level_integral,avatar){
        api.openWin({
            name: 'uplevel_examine_details',
            url: './uplevel_examine_details.html',
            pageParam: {
                id: id,
                level_integral: level_integral,
                name: name,
                level_str: level_str,
                avatar: avatar,
            }
        });
    }

    // 切换分类(申请/审批)
    function changeSort(sort){
        $('#sort span').removeClass('active');
        $('#'+sort).addClass('active');
        if (sort == 'apply') {
            loadApply();
            sortName = 'apply';
        }else{
            loadAuditing();
            sortName = 'auditing';
        }
    }

    // 监听事件 (页面刷新)
    function reloadListener(){
      api.addEventListener({
        name: 'reload',
    }, function(ret, err){
        // alert(sortName)
        if (sortName == 'apply') {
            loadApply();
        }else{
            loadAuditing();
        }
    });
  }

  function user_wallet() {
    api.openWin({
        name: 'user_wallet',
        url: './user_wallet.html',
    });
}

function closeWin(){
    var setSele = 'gotoUserIndex();';
    api.execScript({
        name: 'root',
        script: setSele,
    });
    api.closeToWin({
        name: 'root'
    });
}
</script>
</html>